﻿<template>
  <a-modal
    title="编辑领料单"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入单据编号" v-decorator="['orderNo']" />
        </a-form-item>
        <a-form-item label="总数量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入总数量" style="width: 100%" v-decorator="['orderQuantity']" />
        </a-form-item>
        <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入仓库" v-decorator="['areaName']" />
        </a-form-item>
        <a-form-item label="叫料人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入叫料人" v-decorator="['takeMaterialName']" />
        </a-form-item>
        <a-form-item label="叫料工位" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入叫料工位" v-decorator="['callMaterialStation']" />
        </a-form-item>
        <a-form-item label="收货仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入收货仓库" v-decorator="['shippingWarehouse']" />
        </a-form-item>
        <a-form-item label="收货时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择收货时间" v-decorator="['shippingTime']" @change="onChangeshippingTime"/>
        </a-form-item>
        <a-form-item label="班组" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入班组" v-decorator="['teams']" />
        </a-form-item>
        <a-form-item label="计划号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入计划号" v-decorator="['planNo']" />
        </a-form-item>
        <a-form-item label="工程号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入工程号" v-decorator="['projectNo']" />
        </a-form-item>
        <a-form-item label="船号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入船号" v-decorator="['shipNo']" />
        </a-form-item>
        <a-form-item label="仓号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入仓号" v-decorator="['warehouseNo']" />
        </a-form-item>
        <a-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入工单号" v-decorator="['workOrderNo']" />
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入备注" v-decorator="['remarks']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    ReceiveMaterialOrderEdit
  } from '@/api/modular/main/ReceiveMaterialOrderManage'
  export default {
    data () {
      return {
	    Id: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        shippingTimeDateString: '',
        orderStatusData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true;
		this.Id = record.id;
        this.$nextTick(() => {
        });
        //深度拷贝 移除VUE的监听，防止INDEX页面值变动
        this.record = JSON.parse(JSON.stringify(record))
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              orderNo: record.orderNo,
              orderQuantity: record.orderQuantity,
              areaName: record.areaName,
              takeMaterialName: record.takeMaterialName,
              callMaterialStation: record.callMaterialStation,
              shippingWarehouse: record.shippingWarehouse,
              teams: record.teams,
              planNo: record.planNo,
              projectNo: record.projectNo,
              shipNo: record.shipNo,
              warehouseNo: record.warehouseNo,
              workOrderNo: record.workOrderNo,
              remarks: record.remarks
            }
          )
        })
        this.form.getFieldDecorator('shippingTime', { initialValue: moment(record.shippingTime, 'YYYY-MM-DD') })
        this.shippingTimeDateString = moment(record.shippingTime).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (values[key] == null) continue
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            values.shippingTime = this.shippingTimeDateString
            this.record.shippingTime = this.shippingTimeDateString
            ReceiveMaterialOrderEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      onChangeshippingTime(date, dateString) {
        this.shippingTimeDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
